<?php
//decode by http://奥森世纪/
namespace app\home\controller;

use app\admin\model\Config;
use app\home\model\Member;
use app\home\model\Follow;
use app\home\model\Task;
use app\home\model\TaskJoin;
class User extends Base
{

    public $kc_money = 0;
    
    public function _initialize()
    {
        $config = Config::getInfo();
        if(check_array($config['setting'])){
            $setting = $config['setting'];
            if(!empty($setting['kc_money'])){
                $this->kc_money = round(floatval($setting['kc_money']/100),2);
            }
        }
    }    
    
    public function view($id = 0)
    {
        if (!check_id($id)) {
            message('用户信息不存在', '/home/index.html', 'error');
        }
        $member = Member::getUserInfoById($id);
        if (empty($member)) {
            message('用户信息不存在', '/home/index.html', 'error');
        }
        $params = array();
        $params['uid'] = $member['uid'];
        $params['category_type'] = "all";
        $data = $this->_get_data($params);
        $data['member'] = $member;
        $data['isFollow'] = false;
        if(empty($this->member['uid'])){
        	 message('请登录', '/home/index.html', 'error');
        }
        if ($this->member['uid']) {
            $data['isFollow'] = Follow::getIsFollow($this->member['uid'], $member['uid']);
        }
        $data['task_num'] = Task::getTotalCountByMemberId($member['uid']);
        $data['join_num'] = TaskJoin::getTotalCountByMemberId($member['uid']);
        foreach ($data['tasks'] as  $k=>$v){
            $data['tasks'][$k]['unit_price']=$v['unit_price'] - $v['unit_price']*$this->kc_money;
        }
        return $this->fetch('index', $data);
    }
    private function _get_data($params = array())
    {
        $pszie = 15;
        $tasks = \app\home\model\MyTask::getListByParams($params, $pszie);
        if (request()->isAjax()) {
            if (empty($tasks)) {
                message('没有更多信息', '', 'error');
            }
            $html = $this->fetch('_task_list', ['tasks' => $tasks]);
            message($html, '', 'success');
        }
        $count = \app\home\model\MyTask::getCountByParams($params);
        $pageCount = ceil($count / $pszie);
        return ['tasks' => $tasks, 'pageCount' => $pageCount];
    }
}